home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / goodies.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  293 lines

  1.  
  2. /*
  3.  * goodies.c
  4.  *
  5.  * This file belongs to the goodies of the Forms Library.
  6.  *
  7.  * It contains the soft button box, the messages, etc and the color selector.
  8.  *
  9.  * Written by: Mark Overmars
  10.  *
  11.  * Version 2.1 b
  12.  * Date: Oct  4,  1992
  13.  */
  14.  
  15. #include "gl/gl.h"
  16. #include "gl/device.h"
  17. #include "forms.h"
  18.  
  19. /********** A SOFT BUTTON BOX **********/
  20.  
  21. static FL_FORM *buttonbox;
  22. static FL_OBJECT *box_button[32];
  23. static int buttonbox_created = 0;
  24.  
  25. static void handle_buttonbox(FL_OBJECT *obj, long arg)
  26.   { qenter( (short) (SW0+arg), (short) fl_get_button(obj)); }
  27.  
  28. static void create_buttonbox()
  29. {
  30.   FL_OBJECT *obj;
  31.   int i;
  32.   float xx, yy;
  33.   if (buttonbox_created) return; else buttonbox_created = 1;
  34.   buttonbox = fl_bgn_form(FL_UP_BOX,310.0,310.0);
  35.   xx = 10.0; yy = 260.0;
  36.   for (i=0; i<32; i++)
  37.   {
  38.     if (i==0 || i==28) xx += 50.0;
  39.     box_button[i] = fl_add_button(FL_INOUT_BUTTON,xx,yy,40.0,40.0,"");
  40.     fl_set_object_color(box_button[i],47,43);
  41.     fl_set_call_back(box_button[i],handle_buttonbox,i);
  42.     xx += 50.0;
  43.     if (i==3 || i==9 || i==15 || i==21 || i==27) {yy -= 50.0; xx = 10.0;}
  44.   }
  45.   fl_end_form();
  46. }
  47.  
  48. long fl_show_buttonbox()
  49. /* Shows the buttonbox on the screen */
  50. {
  51.   create_buttonbox();
  52.   return fl_show_form(buttonbox,FL_PLACE_FREE,TRUE,"buttonbox"); 
  53. }
  54.  
  55. void fl_hide_buttonbox()
  56. /* Hides the buttonbox */
  57.   { fl_hide_form(buttonbox); }
  58.  
  59. void fl_set_buttonbox_label(int but, char str[])
  60. /* Sets the label for a button in the buttonbox */
  61.   create_buttonbox();
  62.   if (but<32)
  63.     fl_set_object_label(box_button[but],str);
  64.   else
  65.     fl_set_object_label(box_button[but-SW0],str);
  66. }
  67.   
  68. /********** SOME SIMPLE FORMS **********/
  69.  
  70. static FL_FORM *message_form;
  71. static FL_OBJECT *message_str1, *message_str2, *message_str3,
  72.         *message_but;
  73.  
  74. static FL_FORM *question_form;
  75. static FL_OBJECT *question_str1, *question_str2, *question_str3,
  76.         *question_yes, *question_no;
  77.  
  78. static FL_FORM *choice_form;
  79. static FL_OBJECT *choice_str1, *choice_str2, *choice_str3,
  80.         *choice_but1, *choice_but2, *choice_but3;
  81.  
  82. static FL_FORM *input_form;
  83. static FL_OBJECT *input_str1, *input_value, *input_but;
  84.  
  85. static FL_OBJECT *retobj;
  86.  
  87. static int simpleforms_created = 0;
  88.  
  89. static void create_simpleforms()
  90. /* Make all the forms */
  91. {
  92.   if (simpleforms_created) return; else simpleforms_created = 1;
  93.   message_form = fl_bgn_form(FL_UP_BOX,460.0,130.0);
  94.     message_str1  = fl_add_box(FL_NO_BOX,20.0,95.0,420.0,20.0,"");
  95.     message_str2  = fl_add_box(FL_NO_BOX,20.0,75.0,420.0,20.0,"");
  96.     message_str3  = fl_add_box(FL_NO_BOX,20.0,55.0,420.0,20.0,"");
  97.     message_but  = fl_add_button(FL_RETURN_BUTTON,185.0,15.0,90.0,25.0,"OK");
  98.   fl_end_form();
  99.   question_form = fl_bgn_form(FL_UP_BOX,460.0,130.0);
  100.     question_str1  = fl_add_box(FL_NO_BOX,20.0,95.0,420.0,20.0,"");
  101.     question_str2  = fl_add_box(FL_NO_BOX,20.0,75.0,420.0,20.0,"");
  102.     question_str3  = fl_add_box(FL_NO_BOX,20.0,55.0,420.0,20.0,"");
  103.     question_yes  = fl_add_button(FL_NORMAL_BUTTON,80.0,15.0,90.0,25.0,"Yes");
  104.     fl_set_button_shortcut(question_yes, "yY");
  105.     question_no  = fl_add_button(FL_NORMAL_BUTTON,290.0,15.0,90.0,25.0,"No");
  106.     fl_set_button_shortcut(question_no, "nN");
  107.   fl_end_form();
  108.   choice_form = fl_bgn_form(FL_UP_BOX,460.0,130.0);
  109.     choice_str1  = fl_add_box(FL_NO_BOX,20.0,95.0,420.0,20.0,"");
  110.     choice_str2  = fl_add_box(FL_NO_BOX,20.0,75.0,420.0,20.0,"");
  111.     choice_str3  = fl_add_box(FL_NO_BOX,20.0,55.0,420.0,20.0,"");
  112.     choice_but1  = fl_add_button(FL_NORMAL_BUTTON,40.0,15.0,90.0,25.0,"");
  113.     fl_set_button_shortcut(choice_but1, "1");
  114.     choice_but2  = fl_add_button(FL_NORMAL_BUTTON,185.0,15.0,90.0,25.0,"");
  115.     fl_set_button_shortcut(choice_but2, "2");
  116.     choice_but3  = fl_add_button(FL_NORMAL_BUTTON,330.0,15.0,90.0,25.0,"");
  117.     fl_set_button_shortcut(choice_but3, "3");
  118.   fl_end_form();
  119.   input_form = fl_bgn_form(FL_UP_BOX,460.0,130.0);
  120.     input_str1  = fl_add_box(FL_NO_BOX,20.0,95.0,420.0,20.0,"");
  121.     input_value = fl_add_input(FL_NORMAL_INPUT,30.0,50.0,400.0,30.0,"");
  122.     input_but  = fl_add_button(FL_RETURN_BUTTON,185.0,15.0,90.0,25.0,"OK");
  123.   fl_end_form();
  124. }
  125.  
  126. static int xposit()
  127. {
  128.   int x;
  129.   x = (int) getvaluator(MOUSEX)-230;
  130.   if (x<0) x=0;
  131.   return x;
  132. }
  133.  
  134. static int yposit()
  135. {
  136.   int y;
  137.   y = (int) getvaluator(MOUSEY)-27;
  138.   if (y<0) y=0;
  139.   return y;
  140. }
  141.  
  142. void fl_show_message(char str1[],char str2[],char str3[])
  143. /* Shows a simple message with an OK button */
  144. {
  145.   create_simpleforms();
  146.   if (message_form->visible) return;
  147.   fl_set_object_label(message_str1,str1);
  148.   fl_set_object_label(message_str2,str2);
  149.   fl_set_object_label(message_str3,str3);
  150.   fl_deactivate_all_forms();
  151.   fl_set_form_position(message_form,xposit(),yposit());
  152.   fl_show_form(message_form,FL_PLACE_POSITION,FALSE,"message");
  153.   do retobj = fl_do_only_forms(); while (retobj != message_but);
  154.   fl_hide_form(message_form);
  155.   fl_activate_all_forms();
  156. }
  157.  
  158. int fl_show_question(char str1[],char str2[],char str3[])
  159. /* Shows a question with two buttons, yes, and no */
  160. {
  161.   create_simpleforms();
  162.   fl_set_object_label(question_str1,str1);
  163.   fl_set_object_label(question_str2,str2);
  164.   fl_set_object_label(question_str3,str3);
  165.   fl_deactivate_all_forms();
  166.   fl_set_form_position(question_form,xposit(),yposit());
  167.   fl_show_form(question_form,FL_PLACE_POSITION,FALSE,"question");
  168.   do
  169.     retobj = fl_do_only_forms();
  170.   while (retobj != question_yes && retobj != question_no);
  171.   fl_hide_form(question_form);
  172.   fl_activate_all_forms();
  173.   return (retobj == question_yes);
  174. }
  175.  
  176. const char *
  177. fl_show_input(char str1[], char defstr[])
  178. /* Asks the user for textual input */
  179. {
  180.   create_simpleforms();
  181.   fl_set_object_label(input_str1,str1);
  182.   fl_set_input(input_value,defstr);
  183.   fl_deactivate_all_forms();
  184.   fl_set_form_position(input_form,xposit(),yposit());
  185.   fl_show_form(input_form,FL_PLACE_POSITION,FALSE,"input");
  186.   do retobj = fl_do_only_forms(); while (retobj != input_but);
  187.   fl_hide_form(input_form);
  188.   fl_activate_all_forms();
  189.   return (fl_get_input(input_value));
  190. }
  191.  
  192. int fl_show_choice(char str1[], char str2[], char str3[],
  193.         int numb, char but1[], char but2[], char but3[])
  194. /* Asks the user for a choice */
  195. {
  196.   create_simpleforms();
  197.   fl_set_object_label(choice_str1,str1);
  198.   fl_set_object_label(choice_str2,str2);
  199.   fl_set_object_label(choice_str3,str3);
  200.   fl_hide_object(choice_but1);
  201.   fl_hide_object(choice_but2);
  202.   fl_hide_object(choice_but3);
  203.   switch (numb)
  204.   {
  205.     case 3:
  206.         fl_set_object_label(choice_but3,but3);
  207.         fl_show_object(choice_but3);
  208.     case 2:
  209.         fl_set_object_label(choice_but2,but2);
  210.         fl_show_object(choice_but2);
  211.     case 1:
  212.         fl_set_object_label(choice_but1,but1);
  213.         fl_show_object(choice_but1);
  214.     break;
  215.     default:
  216.     return 0;
  217.   }
  218.   fl_deactivate_all_forms();
  219.   fl_set_form_position(choice_form,xposit(),yposit());
  220.   fl_show_form(choice_form,FL_PLACE_POSITION,FALSE,"choice");
  221.   do
  222.     retobj = fl_do_only_forms();
  223.   while (retobj != choice_but1&& retobj != choice_but2&& retobj != choice_but3);
  224.   fl_hide_form(choice_form);
  225.   fl_activate_all_forms();
  226.   if (retobj == choice_but1) return 1;
  227.   else if (retobj == choice_but2) return 2;
  228.   else return 3;
  229. }
  230.  
  231. /****************** Color Selector Goodie ************************/
  232.  
  233. static FL_FORM *colorform;
  234. static FL_OBJECT *col[64], *next, *prev, *cancel;
  235. static int colorform_created = 0;
  236.  
  237. static void create_colorform()
  238. {
  239.   FL_OBJECT *obj;
  240.   int i,j;
  241.   if (colorform_created) return; else colorform_created = 1;
  242.   colorform = fl_bgn_form(FL_UP_BOX,240.0,220.0);
  243.   for (i=0; i<8; i++) for (j=0; j<8; j++)
  244.   {
  245.     col[8*i+j] = fl_add_button(FL_NORMAL_BUTTON,40.0+j*20.0,190.0-i*20.0,
  246.                     20.0,20.0,"");
  247.     fl_set_object_boxtype(col[8*i+j],FL_BORDER_BOX);
  248.     fl_set_object_lcol(col[8*i+j],7);
  249.   }
  250.   prev = fl_add_button(FL_NORMAL_BUTTON,10.0,50.0,30.0,160.0,"@4");
  251.   next = fl_add_button(FL_NORMAL_BUTTON,200.0,50.0,30.0,160.0,"@6");
  252.   cancel = fl_add_button(FL_NORMAL_BUTTON,40.0,10.0,160.0,30.0,"Cancel");
  253.   fl_end_form();
  254. }
  255.  
  256. static void init_colors(int cc, int thecol)
  257. /* initializes the colors */
  258. {
  259.   int i;
  260.   fl_freeze_form(colorform);
  261.   for (i=0; i<64; i++)
  262.   {
  263.     fl_set_object_color(col[i],cc+i,cc+i);
  264.     fl_set_object_label(col[i],"");
  265.     if (thecol == cc+i) fl_set_object_label(col[i],"@9plus");
  266.   }
  267.   fl_unfreeze_form(colorform);
  268. }
  269.  
  270. int fl_show_colormap(int oldcol)
  271. /* Shows a colormap color selector from which the user can select a color. */
  272. {
  273.   FL_OBJECT *ob;
  274.   int i, cc = 64 * (oldcol/64), ready = 0, thecol =oldcol;
  275.   create_colorform();
  276.   init_colors(cc,thecol);
  277.   fl_set_object_color(cancel,thecol,thecol);
  278.   fl_deactivate_all_forms();
  279.   fl_show_form(colorform,FL_PLACE_MOUSE,FALSE,"colormap");
  280.   while (!ready) 
  281.   {
  282.     ob = fl_do_only_forms();
  283.     if (ob == prev && cc>0) {cc -= 64; init_colors(cc,thecol);}
  284.     else if (ob == next ) {cc += 64; init_colors(cc,thecol);}
  285.     else if (ob == cancel) ready = TRUE;
  286.     else for (i=0; i<64; i++) if (ob == col[i]) {ready = TRUE; thecol = cc+i;}
  287.   }
  288.   fl_hide_form(colorform);
  289.   fl_activate_all_forms();
  290.   return thecol;
  291. }
  292.